<?php
class Menu_Class{
    protected $ci;
    private $admin_db;
	private $menu_master,$menu_detail,$menu_detail_url;
    public function __construct() {
        $this->ci = & get_instance();
        $this->admin_db = $this->ci->load->database('admin',true);
    }

	public function menu($admin_info,$sid=''){

		$table		= 'admin_access';
		$where		= array('status'=>1);
        $query		= $this->admin_db->select()->from($table)->where($where)->order_by('access_order','ASC')->get();
		$access_data= $query->result_array();

		$group_id		=	$admin_info['admin_accessgroup'];
		$admin_access	=	$admin_info['admin_access'];
	    $isSuper		=	$admin_info['admin_type'];//超级权限

		if($isSuper!=1 && !empty($group_id) && $group_id!=0){
			$table		= 'admin_accessgroup';
			$where		= array('group_id'=>$group_id,'status'=>1);
			$query		= $this->admin_db->select()->from($table)->where($where)->limit(1)->get();
			$group_data = $query->row_array();
			if(!empty($group_data)){
				$admin_info['group_access']	=	$group_data['group_access'];
				$admin_access	=	$group_data['group_access'];
			}
		}

		$access	=	explode('|',$admin_access);//分割权限
	    $module	=	(!empty($access[0]))?explode(',',$access[0]):array();//模块
	    $menu	=	(!empty($access[1]))?explode(',',$access[1]):array();//栏目
	    $action	=	(!empty($access[2]))?explode(',',$access[2]):array();//具体操作

 		$list		 =	array();
	    $parent_node =	$this->selectArray($access_data,array('pid'=>'0'),$module,$isSuper);//得到所有的父节点
		if(!empty($parent_node))foreach ($parent_node as $node){
          	$node_details	=	$this->selectArray($access_data,array('pid'=>$node['sid']),$menu,$isSuper,$sid);
			$node['checked']=	0;
          	if(isset($node_details[$sid])){
				$node['checked']	=	1;
          		$this->menu_master	=  $node['access_name'];//主菜单
          	}
          	$node['detail']		=	$node_details;
          	$list[$node['sid']]	=	$node;
	    }

	    $data['admin_name']	=	$admin_info['admin_name'];
	    $data['admin_type']	=	$admin_info['admin_type'];
	    $data['platform']	=	$admin_info['platform'];
	    $data["menu_master"]=	$this->menu_master;
	    $data["menu_detail"]=	$this->menu_detail;
	    $data["menu_detail_url"]=$this->menu_detail_url;
	    $data['list']	=	$list;
	    return $data;
	}
 /**
	 * 安装要求筛选数组
	 *@param  要筛选的数组 $arr
	 * @param 按照键值筛选 $key
	 */
	public function selectArray($arr=array(),$key=array(),$menu,$isSuper,$sid=''){
		if(empty($arr))return array();
		$returnArr=array();
		foreach ($arr as $r){
			$r['sid']==$sid?$r['checked']=1:$r['checked']=0;
			if($r['sid']==$sid){//子菜单
				  $this->menu_detail_url=$r['access_url']."&sid=".$sid;
				  $this->menu_detail=$r['access_name'];
			}
			if(!in_array($r['sid'],$menu)&&$isSuper!='1')continue;
			foreach ($key as $k=>$v){
				if(isset($r[$k])&&$v==$r[$k]){
					$returnArr[$r['sid']]=$r;
				}
			}
		}
		return $returnArr;
	}
	/**
	 * 获取界面的具体操作
	 * @param 界面的sid $sid
	 * @param action操作 $action
	 * @param 所有权限组 $arr
	 * @param 是否超级权限 $isSuper
	 */
	public function action($sid,$act,$arr,$isSuper){
		$act_arr=array();
		foreach ($arr as $r){
			if($r['pid']==$sid){
				if($isSuper==1){
					$act_arr[$r['sid']]=array(
					    'sid'=>$r['sid'],
						'access_name'=>$r['access_name'],
       	  				'access_url'=>$r['access_url'],
       	  				'isConfirm'=>true//是否提示验证
					);
					continue;
				}else{
					if(in_array($r['sid'],$act)){
						$act_arr[$r['sid']]=array(
						    'sid'=>$r['sid'],
							'access_name'=>$r['access_name'],
	       	  				'access_url'=>$r['access_url'],
	       	  				'isConfirm'=>true//是否提示验证
						);
					}
				}
			}
		}
		return $act_arr;
	}
	public function check_access($sid,$_conn,$return=false){
		if(!isset($_COOKIE['myprefix_admin_access'])){
			echo "<script>top.location.href='index.php';</script>";
			exit();
		}	
		if($_COOKIE['myprefix_admin_type']==1){
		   	if(!$return)return array(true,'');
		   	return true;
		}
		$access_data	=	$this->get_one_access($sid,$_conn);
		if(empty($access_data) && $_COOKIE['myprefix_admin_id']==0){
			if(!$return)return array(false,"该权限[".$access_data['access_name']."]不存在");
			return false;
		}
		//该权限已经废除，解除限制了
		if($access_data['status'] != 1){
			if(!$return)return array(false,"该权限[".$access_data['access_name']."]已废除");
			return false;
		}
		$access				=	explode('|',trim($_COOKIE['myprefix_admin_access']));
		$depth				=	$access_data['depth'];
		$access_children	=	isset($access[$depth])?explode(',',$access[$depth]):array();
		if(!in_array($sid,$access_children)&&$_COOKIE['myprefix_admin_type']==0){
			if(!$return)return array(false,"你没有该权限[".$access_data['access_name']." sid : ".$sid."]");
			return false;
		}
		if(!$return)return array(true,'');
		return true;
	}
	public function get_one_access($sid,$_conn){
		if(empty($sid)){
			return array();
		}
		$where	=	"sid='$sid'";
		$sql	=	"SELECT * FROM admin_access WHERE $where LIMIT 1";
		$query	=	mysql_query($sql, $_conn);
		$rs		=	mysql_fetch_assoc($query);
		return $rs;
	}
}